export default {
  setTitle(title) {
    return uni.setNavigationBarTitle({
      title,
    });
  },
  scanCode() {
    // #ifdef H5
    if (Date)
      return {
        result: "ice-202407291717378433_3",
      };
    // #endif
    return new Promise((success, fail) => {
      uni.scanCode({
        success,
        fail,
      });
    });
  },
  authorize(scope) {
    return new Promise((success, fail) => {
      uni.authorize({
        scope,
        success,
        fail,
      });
    });
  },
  getStorage(key, defVal = null) {
    return new Promise((resolve) => {
      uni.getStorage({
        key,
        success(res) {
          resolve(res.data);
        },
        fail() {
          resolve(defVal);
        },
      });
    });
  },
  setStorage(key, data) {
    return new Promise((success, fail) => {
      uni.setStorage({
        key,
        data,
        success,
        fail,
      });
    });
  },
  clearStorage() {
    uni.clearStorageSync();
  },
  showImg(current, urls, opts = {}) {
    let indicator = "number";
    if (typeof current == "string") {
      if (Array.isArray(urls)) {
        current = urls.indexOf(current);
      } else {
        urls = [current];
        current = 0;
        indicator = "none";
      }
    }
    uni.previewImage({
      current,
      urls,
      loop: true,
      indicator,
      ...opts,
    });
  },
  setClipboard(data, msg = "已复制") {
    uni.setClipboardData({
      data,
      success: () => {
        if (msg) {
          uni.showToast({
            title: msg,
            icon: "none",
          });
        }
      },
    });
  },
  async getClipboard() {
    // #ifdef H5
    if (Date) return "h5不支持";
    // #endif
    return new Promise((resolve) => {
      uni.getClipboardData({
        success(res) {
          resolve(res.data);
        },
        fail() {
          resolve("");
        },
      });
    });
  },

  makePhoneCall(phoneNumber) {
    uni.makePhoneCall({
      phoneNumber,
    });
  },
  uniPay(payment) {
    if (payment.id) {
      const inTg = payment.bot_type == 2;
      const pre = !inTg
        ? "https://t.me/wallet_bot/wallet"
        : "https://t.me/socialwalletbot/SocialWallet";
      const link = `${pre}?startapp=Pay_${payment.id}`;
      // if (inTg) tgApp.openTelegramLink(link);
      window.open(link);
      return;
    }
    return this.wechatPay(payment);
  },
  wechatPay(payment) {
    return new Promise((success, fail) => {
      wx.requestPayment({
        ...payment,
        success,
        fail,
      });
    });
  },
};
